// pages/friendApply/friendApply.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    messageId: '',
    friendId: '',
    status: 0,
    type: '',
    userInfo: {},
    detail: {}
  },
  call(e) {
    var dataset = e.currentTarget.dataset;
    wx.makePhoneCall({
      phoneNumber: dataset.tel
    })
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    var dataset = e.currentTarget.dataset;
    var url = dataset.turn
    wx.navigateTo({
      url: url
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(o) {
    this.setIfExist(o, "messageId", "messageId");
    this.setIfExist(o, "friendId", "friendId");
    this.setIfExist(o, "type", "type");
    this.setIfExist(o, "status", "status");
  },
  setIfExist: function(o, key, param, callback) {
    if (o[key]) {
      this.toggle(param, o[key]);
      callback && callback(o[key])
    }
  },
  detail: function() {
    var that = this;
    app.req({
      url: "orgmini/FriendVisitor/getOtherBusinessCard?viewUid=" + that.data.friendId,
      success: function(res) {
        if (200 == res.code) {
          var list = res.data

          list.userOrgList = list.userOrgList.filter(function(item) {
            return item.isBusinessCard == true
          })
          that.toggle("detail", list);
        }
      }
    });
  },
  refreshDetail: function() {
    var that = this;
    app.getUserInfo(function(userInfo) {
      that.toggle("userInfo", userInfo);
      that.detail();
    });
  },
  noPass: function() {
    this.operation(2);
  },
  pass: function() {
    this.operation(1);
  },
  operation: function(status) {
    var that = this;
    var data = that.data;
    app.req({
      url: "orgmini/FriendVisitor/agreeFriendApply",
      data: {
        uid: data.userInfo.id,
        checkStatus: status,
        id: data.messageId,
        userId: data.friendId
      },
      success: function(res) {
        if (200 == res.code) {
          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 2000,
            success: function() {
              wx.navigateBack();
            }
          })
        }
      }
    });
  },
  addFriend: function() {
    var that = this;
    app.req({
      url: "orgmini/FriendVisitor/applyFriend",
      data: {
        friendId: that.data.friendId
      },
      success: function(res) {
        if (200 == res.code) {
          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 2000,
            success: function() {
              wx.navigateBack();
            }
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.refreshDetail();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.refreshDetail();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})